package com.chance.cc.crawler.prod.command.job.domain.commerce.gome.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.commerce.gome.GomeCommonCrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

public class GomeCommentCrawlerScheduleJob extends GomeCommonCrawlerScheduleJob {
    public static void main(String[] args) {
        publishGomeCrawlerScheduleJobInfo();
    }
    public static CrawlerJob publishGomeCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = commentSchedule();

        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("国美商场根据关键字采集所有商品最新评论");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业："+httpPage.getRawText());
        return crawlerJob;
    }
    public static CrawlerJob commentSchedule(){
        String site = "commodity";//product
        String site_biz = "comment";
        String start_url = "https://search.gome.com.cn/search?question=nike&searchType=goods&search_mode=normal&reWrite=true&instock=1";

        CrawlerRequestRecord beforeRecord = CrawlerRequestRecord.builder()
                .startPageRequest("gome_keyword", CrawlerEnum.CrawlerRequestType.turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/"+domain+"/keys?site=SearchKV")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .recordKey(start_url)
                .httpUrl(start_url)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                //.filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                //.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,site_biz,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.getHttpRequest().addExtra("searchKvSourceUrl","https://search.gome.com.cn/search?question=%s&searchType=goods&search_mode=normal&reWrite=true&instock=1");
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);

        CrawlerRecord commentFilterRecord = new CrawlerRecord();
        commentFilterRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        commentFilterRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        commentFilterRecord.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-",filter,domain,"comment")));
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(commentFilterRecord));

        String requestQueueName = StringUtils.joinWith("-", "crawler",domain, site, site_biz, "queue");
        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(5)
                .triggerInfo(
                        domain,
//                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//gome-product-comment-realtime
                        StringUtils.joinWith("-", site, site_biz, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))//crawler-gome-product-realtime-queue
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline(null, kafkaTopic, null)
                //.fileResultPipeline(null, "/data/chance_crawler_runner/logs/node/gome.log", false)//
                .supportRecord(beforeRecord)
                .requestRecord(crawlerRequestRecord)
                .build();
        return crawlerJob;
    }
}
